# ~*~ coding: utf-8 ~*~
from ..models import Asset, AdminUser
from apps.extensions import db

__all__ = ['db_to_ansible_of_asset_admin_users', ]


def db_to_ansible_of_asset_admin_users(assets):
    asset_ids = [asset.id for asset in assets]
    result = db.session.query(Asset, AdminUser). \
        join(AdminUser, Asset.admin_user_id == AdminUser.id). \
        filter(Asset.id.in_(asset_ids)). \
        all()

    if not result:
        return None

    assets = list(zip(*result))[0]
    admin_users = list(zip(*result))[1]

    run_as = [admin_user.auth_info for admin_user in admin_users]
    become = [admin_user.become_info for admin_user in admin_users]
    hosts = [{'hostname': asset.hostname, 'ip': asset.manage_ip, 'port': asset.port} for asset in assets]
    return hosts, run_as, become
